// Morphological.cpp : implementation file
//

#include "stdafx.h"
#include "ImageLab.h"
#include "Morphological.h"
#include "ChildView.h"


// CMorphological dialog

IMPLEMENT_DYNAMIC(CMorphological, CDialog)

CMorphological::CMorphological(CWnd* pParent /*=NULL*/)
	: CDialog(CMorphological::IDD, pParent)
{
	ASSERT(pParent!=NULL);
	m_parent = pParent;
	m_nID = CMorphological::IDD;
}

CMorphological::~CMorphological()
{
}

//do data exchange of the dialog
void CMorphological::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

//call the create to create the dialog
BOOL CMorphological::Create()
{
	BOOL isCreated = CDialog::Create(m_nID, m_parent);
	return isCreated;
}

//call the post destroy function to destroy the dialog
void CMorphological::PostNcDestroy()
{
	CDialog::DestroyWindow();
	if(m_parent)
	{
		((CChildView*)m_parent)->img_mor = NULL;
	}
	delete this;
	return;
}

//destroy the dialog
void CMorphological::DialogDestroy()
{
	CDialog::DestroyWindow();
	if(m_parent!=NULL)
	{
		((CChildView*)m_parent)->img_mor = NULL;
	}
	return;
}

BEGIN_MESSAGE_MAP(CMorphological, CDialog)
	ON_BN_CLICKED(IDC_BUTTON_ERODE, &CMorphological::OnBnClickedButtonErode)
	ON_BN_CLICKED(IDC_BUTTON_DILATE, &CMorphological::OnBnClickedButtonDilate)
	ON_BN_CLICKED(IDC_BUTTON_INVERT, &CMorphological::OnBnClickedButtonInvert)
	ON_BN_CLICKED(IDC_BUTTON_BINARY, &CMorphological::OnBnClickedButtonBinary)
	ON_BN_CLICKED(IDC_BUTTON_COMBINE, &CMorphological::OnBnClickedButtonCombine)
	ON_BN_CLICKED(IDC_BUTTON_CLOSING, &CMorphological::OnBnClickedButtonClosing)
	ON_BN_CLICKED(IDC_BUTTON_OPENING, &CMorphological::OnBnClickedButtonOpening)
	ON_BN_CLICKED(IDC_BUTTON_PLOTEDGE, &CMorphological::OnBnClickedButtonPlotedge)
END_MESSAGE_MAP()


// CMorphological message handlers

//erode the image
void CMorphological::OnBnClickedButtonErode()
{
	//read the number of rounds
	CEdit* pEdit = (CEdit*)CMorphological::GetDlgItem(IDC_EDIT2);
	LPTSTR m_text = new WCHAR[256];
	int m_line = pEdit->GetLine(0, m_text);
	if(m_line>=256)
	{
		::AfxMessageBox(CString("number too big"), MB_OK, 0);
		return;
	}
	m_text[m_line] = '\0';
	int m_round = _wtoi(m_text);
	if(m_round>=100)
	{
		::AfxMessageBox(CString("Man, don't be stupid\nPick a smaller number"), MB_OK, 0);
		return;
	}
	//erode
	for(int i=0; i<m_round; i++)
	{
		((CChildView*)m_parent)->ImageErode();
	}
	free(m_text);
	return;
}

//dilate the image
void CMorphological::OnBnClickedButtonDilate()
{
	//read the number of rounds
	CEdit* pEdit = (CEdit*)CMorphological::GetDlgItem(IDC_EDIT2);
	LPTSTR m_text = new WCHAR[256];
	int m_line = pEdit->GetLine(0, m_text);
	if(m_line>=256)
	{
		::AfxMessageBox(CString("number too big"), MB_OK, 0);
		return;
	}
	m_text[m_line] = '\0';
	int m_round = _wtoi(m_text);
	if(m_round>=100)
	{
		::AfxMessageBox(CString("Man, don't be stupid\nPick a smaller number"), MB_OK, 0);
		return;
	}
	//dilate
	for(int i=0; i<m_round; i++)
	{
		((CChildView*)m_parent)->ImageDialate();
	}
	return;
}

//invert the image
void CMorphological::OnBnClickedButtonInvert()
{
	((CChildView*)m_parent)->OnImageInvert();
	return;
}

//show the image
void CMorphological::OnBnClickedButtonBinary()
{
	((CChildView*)m_parent)->OnImageBinary();
	return;
}

//combine the image with the original image
void CMorphological::OnBnClickedButtonCombine()
{
	((CChildView*)m_parent)->OnCombineBinary();
	return;
}

//close the image
void CMorphological::OnBnClickedButtonClosing()
{
	//read the number of rounds
	CEdit* pEdit = (CEdit*)CMorphological::GetDlgItem(IDC_EDIT2);
	LPTSTR m_text = new WCHAR[256];
	int m_line = pEdit->GetLine(0, m_text);
	if(m_line>=256)
	{
		::AfxMessageBox(CString("number too big"), MB_OK, 0);
		return;
	}
	m_text[m_line] = '\0';
	int m_round = _wtoi(m_text);
	if(m_round>=100)
	{
		::AfxMessageBox(CString("Man, don't be stupid\nPick a smaller number"), MB_OK, 0);
		return;
	}
	//close the image
	for(int i=0; i<m_round; i++)
	{
		((CChildView*)m_parent)->ImageDialate();
	}
	for(int i=0; i<m_round; i++)
	{
		((CChildView*)m_parent)->ImageErode();
	}
	return;
}

void CMorphological::OnBnClickedButtonOpening()
{
	//read the number of rounds
	CEdit* pEdit = (CEdit*)CMorphological::GetDlgItem(IDC_EDIT2);
	LPTSTR m_text = new WCHAR[256];
	int m_line = pEdit->GetLine(0, m_text);
	if(m_line>=256)
	{
		::AfxMessageBox(CString("number too big"), MB_OK, 0);
		return;
	}
	m_text[m_line] = '\0';
	int m_round = _wtoi(m_text);
	if(m_round>=100)
	{
		::AfxMessageBox(CString("Man, don't be stupid\nPick a smaller number"), MB_OK, 0);
		return;
	}
	//open the image
	for(int i=0; i<m_round; i++)
	{
		((CChildView*)m_parent)->ImageErode();
	}
	for(int i=0; i<m_round; i++)
	{
		((CChildView*)m_parent)->ImageDialate();
	}
	return;
}

//plot the edge of the binay from the original image
void CMorphological::OnBnClickedButtonPlotedge()
{
	((CChildView*)m_parent)->OnRecover();
	((CChildView*)m_parent)->OnPlotEdge();
	return;
}

//on Press Enter
void CMorphological::OnOK()
{
	return;
}